package com.plan.controller.card;

import com.plan.base.constant.BaseCode;
import com.plan.base.constant.Constant;
import com.plan.base.entity.Result;
import com.plan.controller.user.response.UserVO;
import com.plan.controller.card.request.DayPlanStatPageListReq;
import com.plan.controller.card.response.DayPlanStatPageListRes;
import com.plan.service.card.DayPlanStatService;
import com.plan.service.card.TeamService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 日统计
 *
 * @author zcl
 */
@RestController
@RequestMapping("dayPlanStat/")
public class DayPlanStatController {
    @Resource
    private TeamService teamService;
    @Resource
    private DayPlanStatService dayPlanStatService;

    /**
     * 日统计列表
     */
    @PostMapping(value = "pageList")
    public Result<List<DayPlanStatPageListRes>> pageList(@RequestBody DayPlanStatPageListReq req, HttpServletRequest httpRequest) {
        if (req.getTeamId() == null || req.getCardId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        if (req.getPageSize() > Constant.CLIENT_MAX_PAGE_SIZE) {
            req.setPageSize(Constant.CLIENT_MAX_PAGE_SIZE);
        }
        UserVO user = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(req.getTeamId(), user.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        List<DayPlanStatPageListRes> res = dayPlanStatService.pageList(req);
        return Result.ok(res);
    }
}
